package com.myway.controller;

import com.alibaba.fastjson.JSON;
import com.myway.dto.common.JsonData;
import com.myway.pojo.TabMemberInfo;
import com.myway.service.MemberService;
import com.myway.utils.BASE64Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 会员操作
 */
@Controller
@RequestMapping("/en/member")
public class EnMemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 添加会员，添加成功跳入登陆页面
     *
     * @param tabMemberInfo
     * @return
     */
    @RequestMapping("/addMember")
    @ResponseBody
    public JsonData addMember(TabMemberInfo tabMemberInfo) {
        tabMemberInfo.setMembPwd(BASE64Util.decode(tabMemberInfo.getMembPwd()));
        return memberService.addMember(tabMemberInfo);
    }

    /**
     * 修改会员信息
     *
     * @param tabMemberInfo
     * @return
     */
    @RequestMapping("/updateMember")
    @ResponseBody
    public JsonData updateMember(TabMemberInfo tabMemberInfo) {

        System.out.println("修改会员信息：==" + JSON.toJSON(tabMemberInfo));
        return memberService.updateMember(tabMemberInfo);
    }

    /**
     * 修改会员时的手机号，邮箱，证件号判断是否已存在
     *
     * @param tabMemberInfo
     * @return
     */
    @RequestMapping("/upIsRepetition")
    @ResponseBody
    public boolean updateIsRepetition(TabMemberInfo tabMemberInfo) {
        //tabMemberInfo中只存放相应字段和用户的membId，然后验证是否已经存在了
        return memberService.updateIsRepetition(tabMemberInfo);
    }

    /**
     * 添加会员时的手机号，邮箱，证件号判断是否已存在
     *
     * @param tabMemberInfo
     * @return
     */
    @RequestMapping("/adIsRepetition")
    @ResponseBody
    public boolean addIsRepetition(TabMemberInfo tabMemberInfo) {
        //tabMemberInfo中只存放相应字段，然后验证是否已经存在了
        return memberService.addIsRepetition(tabMemberInfo);
    }

    /**
     * 逻辑删除旅客常用联系人
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteFrequentFlyer")
    @ResponseBody
    public boolean deleteFrequentFlyer(Long id){
        try{
            memberService.deleteFrequentFlyer(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
